package com.ksd1.notes.markdown;

import java.util.List;

import com.ksd1.notes.markdown.Markdown.MDLine;
import com.ksd1.notes.markdown.Markdown.MDWord;

import android.graphics.Color;
import android.text.Layout;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.AlignmentSpan;
import android.text.style.BulletSpan;
import android.text.style.LeadingMarginSpan;
import android.text.style.QuoteSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;
import android.text.style.TypefaceSpan;

public class MDFormatter {

    //可以改变文本的样式
    private final SpannableStringBuilder mBuilder = new SpannableStringBuilder();

    /**
     * @param lines 需要拼接的所有行
     */
    public MDFormatter(List<MDLine> lines) {
        for (MDLine line : lines) {
            format(line);
        }
        mBuilder.setSpan(new TypefaceSpan("monospace"), 0, mBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    public SpannableStringBuilder getFormattedContent() {
        return mBuilder;
    }


    /**
     * @param line 需要拼接的行
     */
    protected void format(MDLine line) {
        int start = mBuilder.length();//拼接的位置

        for (MDWord word : line.mMDWords) {//遍历行切分开的所有词
            int index = mBuilder.length();
            mBuilder.append(word.mRawContent);
            mBuilder.setSpan(getSpan(word.mFormat), index, mBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        mBuilder.append("\n");

        //设置行的样式
        //有序.无序.引用 都要设置margin
        if (line.mFormat == Markdown.MD_FMT_ORDER_LIST ||
                line.mFormat == Markdown.MD_FMT_UNORDER_LIST ||
                line.mFormat == Markdown.MD_FMT_QUOTE) {
            mBuilder.setSpan(new LeadingMarginSpan.Standard(40), start, mBuilder.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        }
        if (line.mFormat != Markdown.MD_FMT_TEXT) {
            mBuilder.setSpan(getSpan(line.mFormat), start, mBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);//为mBuilder设置样式
        }
    }

    /**
     * 通过编号获取样式
     * @param format 样式
     * @return span样式对象
     */
    protected static Object getSpan(int format) {
        switch (format) {
            case Markdown.MD_FMT_TEXT:                                  //无样式
                return new RelativeSizeSpan(1.1f);
            case Markdown.MD_FMT_HEADER1:                               //一级标题
                return new RelativeSizeSpan(1.5f);
            case Markdown.MD_FMT_HEADER2:                               //二级标题
                return new RelativeSizeSpan(1.4f);
            case Markdown.MD_FMT_HEADER3:                               //三级标题
                return new RelativeSizeSpan(1.3f);
            case Markdown.MD_FMT_QUOTE:                                 //引用
                return new QuoteSpan(Color.GRAY);
            case Markdown.MD_FMT_ITALIC:                                //斜体
                return new StyleSpan(android.graphics.Typeface.ITALIC);
            case Markdown.MD_FMT_BOLD:                                  //加粗
                return new StyleSpan(android.graphics.Typeface.BOLD);
            case Markdown.MD_FMT_CENTER:                                //居中
                return new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER);
            case Markdown.MD_FMT_UNORDER_LIST:                          //无序列表
                return new BulletSpan(10, Color.BLACK);
            case Markdown.MD_FMT_ORDER_LIST:                            //有序列表
                return new BulletSpan(10, Color.TRANSPARENT);
            default:
                break;
        }
        return null;
    }
}
